package com.aver.restful;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

@Component
@Path("/timeoftheday")
public class TimeOfTheDayServiceImpl implements TimeOfTheDayService {
	private static String PATTERN = "MM.dd.yyyy HH:mm:ss";

	@GET
	@Produces("text/plain")
	@Path("/asplaintext")
	public String getTimeOfTheDay() {
		SimpleDateFormat df = new SimpleDateFormat(PATTERN);
		return df.format(Calendar.getInstance().getTime());
	}

	@GET
	@Produces("application/xml")
	@Path("/asxml/{name}/")
	public Time getTimeOfTheDayInXML(@PathParam("name") String name) {
		SimpleDateFormat df = new SimpleDateFormat(PATTERN);
		Time t = new Time();
		t.setName(name);
		t.setTime(df.format(Calendar.getInstance().getTime()));
		return t;
	}

	@GET
	@Produces("application/json")
	@Path("/asjson/{name}/")
	public Time getTimeOfTheDayInJSON(@PathParam("name") String name) {
		SimpleDateFormat df = new SimpleDateFormat(PATTERN);
		Time t = new Time();
		t.setName(name);
		t.setTime(df.format(Calendar.getInstance().getTime()));
		return t;
	}
	
	
	@POST 
	@Path("/hello")
	@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML})
	public Response helloMan(String name) {
		// process the customer pojo in memory or db
		String result = "Hello man: " + name;
		return Response.status(201).entity(result).build();
	}
}